<?php
/**
 * 数据库
 * Created by wzb.
 * User: wzb
 * time:2017/08/15
 */
class DB
{
  public $db;
  function __construct()
  {
    echo "setp: connect db\n";
    try {
    $this->db = new PDO("mysql:host=rm-vy15f520u56n44eew985.mysql.zhangbei.rds.aliyuncs.com;dbname=dulizhan","ry_aliexpress","Rymht321123!@");
    $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $this->db->query("set character set 'UTF8'");
    $status = $this->pdo_ping($dbconn=$this->db);
	}catch(Exception $e){
		echo $e->getMessage();
		echo '!!!PDO<pre>'; var_dump($this->db); echo '</pre>'; die;
	}
    if(!$status){
      echo "connect error!";
      exit;
    }
  }

/**
 * 检查连接是否可用
 * @param  Link $dbconn 数据库连接
 * @return Boolean
 */
function pdo_ping($dbconn){
    try{
        $dbconn->getAttribute(PDO::ATTR_SERVER_INFO);
    } catch (PDOException $e) {
        if(strpos($e->getMessage(), 'MySQL server has gone away')!==false){
            return false;
        }
    }
    return true;
}

//插入
function Getinsert($arrData,$table,$sync_data) {
    $name = $values = '';
    $flag = $flagV = 1;
    $true = is_array( current($arrData) );//判断是否一次插入多条数据
    if($true) {
        //构建插入多条数据的sql语句
        foreach($arrData as $arr) {
            $values .= $flag ? '(' : ',(';
            foreach($arr as $key => $value) {
                if($flagV) {
                    if($flag) $name .= "$key";
                    $values .= "'$value'";
                    $flagV = 0;
                } else {
                    if($flag) $name .= ",$key";
                    $values .= ",'$value'";
                }
            }
            $values .= ') ';
            $flag = 0;
            $flagV = 1;
        }




    } else {
        //构建插入单条数据的sql语句
        foreach($arrData as $key => $value) {
            if($flagV) {
                $name = "$key";
                $values = "('".addslashes($value)."'";
                $flagV = 0;
            } else {
                $name .= ",$key";
                $values .= ",'".addslashes($value)."'";
            }
        }
        $values .= ") ";
       $flagV2 = 1;
         foreach($arrData as $key => $value) {
            if($flagV2) {
                $name = "$key";
                $values_du = "$key='".addslashes($value)."'";
                $flagV2 = 0;
            } else {
                $name .= ",$key";
                $values_du .= ",$key='".addslashes($value)."'";
            }
        }
    }
    $strSql = "insert into $table ($name) values $values ON DUPLICATE KEY UPDATE $values_du";
    echo "语句$strSql\n";
//    echo '<pre>'; print_r($strSql); echo '</pre>'; die;
    $result = $this->db->exec($strSql);
    echo "影响$result\n";
   // if( $result > 0 && $sync_data !== "0") {
    //  sync_info($sync_data,$db);
   // }
    if($result > 0){
      $return_code = '0';
      if($sync_data !== "0"){
        $this->sync_info($sync_data,$db);
      }
    return $return_code;
  }

}

//更新
function update($table,$arrData,$filed,$fileddata) {
    $flag = 1;
    foreach($arrData as $key => $value) {
        if($flag) {
            $data = "$key = '$value'";
            $flag = 0;
        } else $data .= ",$key = '$value'";
    }
    $strSql = "update $table set $data where $filed='$fileddata'";
    echo "***$strSql***\n";
    $result =$this->db->exec($strSql);
    if($result) {
        return true;
    }
    return false;
}

//是否需要更新  $condition = 'payment_last_trans_id=$payment_last_trans_id';
/*
function is_insert($increment_id,$arrData,$table,$db,$sync_data){
       $xc_sql = "select id from $table where increment_id='$increment_id'";
       echo "$xc_sql";
       $num = $db->query($xc_sql);
       $row = $num->fetch();
       if($row > 1){
          //更新
          $condition = "increment_id = $increment_id";
          //update($table,$arrData,$condition,$db,$sync_data);
       }else{
          //插入
          Getinsert($table,$arrData,$db,$sync_data);
       }



}
*/

function sync_info($sync_data){
  $updatetime = $sync_data['updatetime'];
  $new_add_num = $sync_data['new_add_num'];
  $pagenumber = $sync_data['pagenumber'];
  $store_name = $sync_data['store_name'];
  $totalnums = $sync_data['lasttotalnums'];
  $fornum_totnum = $sync_data['fornum_totnum'];
  $strSql = "insert into dlz_sync_ordernums (updatetime,new_add_num,pagenumber,store_name,lasttotalnums,fornum_totnum) values ('$updatetime','$new_add_num','$pagenumber','$store_name','$totalnums','$fornum_totnum') ON DUPLICATE KEY UPDATE updatetime='$updatetime',new_add_num='$new_add_num',pagenumber='$pagenumber',store_name='$store_name',lasttotalnums='$totalnums',fornum_totnum='$fornum_totnum'";
    echo "$strSql\n";
     $result = $this->db->exec($strSql);
    if($result > 0){
            $return_code = '1';
           return $return_code;
    }
}


}

